package com.mobile.collect.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v7.widget.ListPopupWindow;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.mobile.collect.R;
import com.mobile.collect.interfaces.PopWindowInterface;

import java.util.ArrayList;

/**
 * @author: Kongzue
 * @github: https://github.com/kongzue/
 * @homepage: http://kongzue.com/
 * @mail: myzcxhh@live.cn
 * @createTime: 2019/9/29 13:35
 */
public class PopWindowTextView extends android.support.v7.widget.AppCompatTextView implements PopWindowInterface {
    
    public PopWindowTextView(Context context) {
        super(context);
        init(context, null);
    }
    
    public PopWindowTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }
    
    public PopWindowTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }
    
    private String[] menuList;
    
    private void init(Context context, AttributeSet attrs) {
        String menuListCache = null;
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PopWindowEditText);
            menuListCache = typedArray.getString(R.styleable.PopWindowEditText_menu);
            typedArray.recycle();
        }
        if (menuListCache != null && menuListCache.length() > 0) {
            if (menuListCache.contains(",")) {
                menuList = menuListCache.split(",");
            }
        }
        if (!isInEditMode()) {
            setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    showMenu();
                }
            });
        }
    }
    
    public void showMenu() {
        needMenu = true;
        showListPopWindow("");
    }
    
    private ListPopupWindow listPopupWindow;
    private boolean needMenu = true;
    ArrayList<String> menu;
    private void showListPopWindow(String inputString) {
        Log.i(">>>", "isEnabled: " + isEnabled());
        if (!isEnabled()){
            return;
        }
        if (menuList != null && menuList.length != 0) {
            menu = new ArrayList<>();
            for (String s : menuList) {
                if (inputString.length() == 0) {
                    menu.add(s);
                } else {
                    if (s.toLowerCase().contains(inputString.toLowerCase())) {
                        menu.add(s);
                    }
                }
            }
            if (listPopupWindow != null) {
                listPopupWindow.dismiss();
            }
            if (menu.size() == 0) {
                return;
            }
            
            listPopupWindow = new ListPopupWindow(getContext());
            listPopupWindow.setAdapter(
                    new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, menu)
            );
            listPopupWindow.setAnchorView(this);//以哪个控件为基准，在该处以mEditText为基准
            listPopupWindow.setModal(false);
            
            listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {//设置项点击监听
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    needMenu = false;
                    setText(menu.get(i));//把选择的选项内容展示在EditText上
                    listPopupWindow.dismiss();//如果已经选择了，隐藏起来
                }
            });
            listPopupWindow.show();//把ListPopWindow展示出来
        }
    }
    
    public PopWindowTextView setMenuList(String[] menuList) {
        this.menuList = menuList;
        return this;
    }
    
    public PopWindowTextView setMenuList(String menuListCache) {
        if (menuListCache != null && menuListCache.length() > 0) {
            if (menuListCache.contains(",")) {
                menuList = menuListCache.split(",");
            }
        }
        return this;
    }
    
    public void setMenuList(ArrayList<String> descriptionList) {
        this.menuList = descriptionList.toArray(new String[descriptionList.size()]);
    }
}
